<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="部门名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--新增、编辑界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门编号" prop="sn">
                    <el-input v-model="addForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">启用</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="负责人">
                    <!--<el-input v-model="addForm.manager_id" auto-complete="off"></el-input>-->
                    <!--下拉框-->
                    <el-select v-model="addForm.manager" filterable value-key="id" placeholder="请选择">
                        <el-option
                                v-for="emp in employees"
                                :key="emp.id"
                                :label="emp.username"
                                :value="emp">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="上级部门">
                    <!--<el-input v-model="addForm.parent_id" auto-complete="off"></el-input>-->
                    <!--下拉框-->
                    <el-select v-model="addForm.parent" value-key="id" placeholder="请选择">
                        <el-option
                                v-for="dept in parents"
                                :key="dept.id"
                                :label="dept.name"
                                :value="dept">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
        <!--列表 -->
        <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="部门名称" width="120" >
            </el-table-column>
            <el-table-column prop="sn" label="部门编号" width="100" >
            </el-table-column>
            <el-table-column prop="state" label="状态" width="100" :formatter="formatState" sortable>
            </el-table-column>
            <el-table-column prop="manager.username" label="负责人" width="120" >
            </el-table-column>
            <el-table-column prop="parent.name" label="上级部门" min-width="180" >
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>
    </section>
</template>
<script>
    export default {
        data(){
            return {
                listLoading:false, // 开启加载效果
                listData:[],
                sels:[],
                total:0, // 总条数
                query:{
                    pageSize:5,
                    currentPage:1,
                    keywords:''
                },
                addFormVisible: false, // 默认不显示弹出框
                addLoading: false,
                addFormRules: {
                    // 验证规则
                    // required: true：  要求：必填
                    // message：  出错时提示什么消息
                    // trigger:'blur'  触发规则：
                    name: [
                        { required: true, message: '请输入部门名称', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入部门编号', trigger: 'blur' }
                    ]

                },
                // 数据双向绑定
                  addForm :{
                    sn: '',
                    name: '',
                    state: 1,
                    manager: null,
                    parent: null
                },
                employees: [],
                parents: []
            }
        },
        methods:{
            // 批量勾选
            selsChange(val){
                this.sels = val;
            },
            //性别显示转换
            formatState: function (row, column) {
                return row.state == 1 ? '启用' : row.state == 0 ? '关闭' : '未知';
            },
            loadListData(){
                // 发送请求
                this.$http.post("/dept",this.query).then(res=>{
                    // console.debug(res);
                    this.listData = res.data.data;
                    this.total = res.data.total;
                })
            },
            loadEmpData(){
              // 发送请求
              this.$http.get("/emp").then(res=>{
                  console.debug(res);
                  this.employees = res.data;
              })
            },
            loadDeptData(){
              // 发送请求
              this.$http.get("/dept").then(res=>{
                  console.debug(res);
                  this.parents = res.data;
              })
            },
            // 批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id);
                // 弹出确认框
                this.$confirm('确认删除？', '提示', {
                        confirmButtonText: '删除',
                        cancelButtonText: '取消',
                        type: 'warning'
                }).then(()=>{
                    // 发送请求
                    this.$http.patch("/dept/",ids).then(res=>{
                        // 返回信息
                        let {success,message}=res.data;
                        console.debug(success,message);
                        if (success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                        } else {
                            this.$message.error('网络异常');
                        }
                        // 重新加载列表
                        this.loadListData();
                    })
                }).catch(()=>{  // 选择取消回调方法
                    this.$message('已取消');
                })
            },
            // 分页方法
            handleCurrentChange(val){
                this.query.currentPage=val;
                this.loadListData();
            },
            // 高级查询
            search(){
                // 设置初始页面为1
                this.query.currentPage=1;
                // 页面加载
                this.loadListData();
            },
            // 添加
            handleAdd(){
                // 清空弹出框
                this.addForm={
                    sn: '',
                    name: '',
                    // 设置状态为默认开启
                    state: 1,
                    manager_id: null,
                    parent_id: null
                };
                // 弹出框
                this.addFormVisible = true;
            },
            // 修改编辑
            handleEdit(index, row){
                console.debug(row);
                // 弹出框
                this.addFormVisible = true;
                // 数据回显，Object.assign(a,b) 复制方法，将b复制给a，重复的覆盖，没有的新添
                this.addForm = Object.assign({},row);
            },
            // 删除
            handleDel(index, row) {
                // 弹出确认框
                this.$confirm('确认删除？', '提示', {
                    confirmButtonText: '删除',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{ // 选择确认的回调方法
                    // 发送请求
                    this.$http.delete("/dept/"+row.id).then(res=>{
                        // 返回信息
                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                        } else {
                            this.$message.error('网络异常');
                        }
                        console.debug(success,message);
                        // 设置初始页面为1
                        this.query.currentPage=1;
                        // 重新加载列表
                        this.loadListData();
                    })
                }).catch(()=>{  // 选择取消回调方法
                    this.$message('已取消');
                })
            },
            // 提交
            addSubmit(){
                listLoading: true // 开启加载效果
                // 发送请求
                this.$http.put("dept",this.addForm).then(res=>{
                    console.debug(res);
                    // 返回信息
                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: message,
                            type: 'success'
                        });
                    } else {
                        this.$message.error('网络异常');
                    }
                    listLoading: false // 关闭加载效果
                    // 关闭弹出框
                    this.addFormVisible = false;
                    // 重新加载页面
                    this.loadListData();
                }).catch(()=>{  // 选择取消回调方法
                    this.$message('已取消');
                })
            }
        },
        mounted(){
            // 页面加载
            this.loadListData();
            // 加载负责人下拉框数据
            this.loadEmpData();
            // 加载上级部门下拉框数据
            this.loadDeptData()
        }
    }
</script>

<style>

</style>